package com.ikun.admin.common.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ikun.admin.modules.auth.utils.SecurityUtil;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @ClassName MyMetaObjectHandler
 * @Description:
 * @CreateDate: 2024/7/1 22:48
 */
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByName("createTime", LocalDateTime.now(), metaObject);
        Long currentUserId = SecurityUtil.getCurrentUserId();
        if(Objects.nonNull(currentUserId)){
            this.setFieldValByName("creator",currentUserId , metaObject);
        }


    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
        Long currentUserId = SecurityUtil.getCurrentUserId();
        if(Objects.nonNull(currentUserId)){
            this.setFieldValByName("updater",currentUserId , metaObject);
        }
    }
}
